package com.sfac.javaSpringBoot.modules.common.service.impl;

import com.sfac.javaSpringBoot.config.ResourcesConfigerBean;
import com.sfac.javaSpringBoot.modules.common.entity.ImageType;
import com.sfac.javaSpringBoot.modules.common.entity.Result;
import com.sfac.javaSpringBoot.modules.common.service.ImageService;
import com.sfac.javaSpringBoot.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Service
public class ImageServiceImpl implements ImageService {

    @Autowired
    private ResourcesConfigerBean configerBean;

    @Value("${artifactId}")
    private String artifactId;
    @Override
    public Result<String> uploadImage(MultipartFile file, String imageType) {

        /*
        * 第一步判断文件是否为空
        * 判断文件是否为图片
        * 获取图片类型的相关信息（枚举，imageType的类型，大的小的封装成一个属性）
        * 构建目标文件的绝对地址   d:/upload/+artifactId+imageType+图片名字
        * 构建目标对象 的相对地址   /upload/+artifactId+imageType+图片名字
        * 写图片到目标对象
        * 变更图片的尺寸---可选的
        * 返回result对象
        * */
        //第一步判断文件是否为空
        if (file.isEmpty()){
            return new Result<>(Result.ResultCode.FAILED.code, "Image is null.");
        }
        // 判断文件是否为图片
        if (!FileUtil.isImage(file)){
            return new Result<>(Result.ResultCode.FAILED.code, "file is not image.");
        }
        // 获取图片类型枚举对象
        ImageType imageTypeMenu = ImageType.getImageTypeByName(imageType);
        // 获取图片类型枚举对象
        String imageName = String.format("%s.%s", System.currentTimeMillis(), FileUtil.getFileType(file.getOriginalFilename()));

        String destFolder = String.format("%s%s/%s",
                System.getProperty("os.name").toLowerCase().startsWith("win")?
                        configerBean.getWindowsLocation():configerBean.getLinuxLocation(),
                artifactId,
                imageType);
        File destFolderFile = new File(destFolder);
        if (!destFolderFile.exists()){
            destFolderFile.mkdirs();
        }
        String absolutePath = String.format("%s/%s",
                destFolder,
                imageName);

        String relatePath = String.format("%s%s/%s/%s",
                configerBean.getPath(),
                artifactId,
                imageType,
                imageName);


        try {
            File destFile = new File(absolutePath);
            file.transferTo(destFile);

            //变更图片尺寸
            FileUtil.changeImageSize(absolutePath, absolutePath, imageTypeMenu.length, imageTypeMenu.height);
            //返回result对象
            return new Result<>(Result.ResultCode.SUCCESS.code, "Upload image success.", relatePath);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result<>(Result.ResultCode.FAILED.code, "Upload image failed.");
        }
    }
}
